﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using CustomersSuppliersBusinessLayer;
using CustomersSuppliersDataLayer;
using System.Collections;
using WebHelper.Helpers;

namespace CustomersSuppliersWebApplication
{
    public partial class WebFormDealers : WebFormBase
    {
        public Guid? DealerType_ID
        {
            get
            {
                return string.IsNullOrEmpty(this.Request[BLDealer.PropertiesNames.DealerType_ID]) ? (Guid?)null : new Guid(this.Request[BLDealer.PropertiesNames.DealerType_ID]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                InitializeTelerikRequest(this.RadTextBoxSearch, "onkeyup");
                int index = CashVariables.DealersTypeCollection.FindByIentifier(this.DealerType_ID);
                if (index != -1)
                {
                    this.PageTitle = CashVariables.DealersTypeCollection[index].DealerType_NameAr;
                }
                this.getImportanceFilter();
                this.getTypeOfTradeFilter();
                this.getProductsFilter();
                this.getDealersClassificationsFilter();
                this.getCountryFilter();
            }
        }
        protected void RadTextBoxSearch_TextChanged(object sender, EventArgs e)
        {
            this.getDealers();
            this.RadGridResults.Rebind();
        }
        protected void RadComboBox_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            this.getDealers();
            this.RadGridResults.Rebind();
        }
        protected void RadDatePicker_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            this.getDealers();
            this.RadGridResults.Rebind();
        }
        protected void RadGridResults_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            this.getDealers();
        }
        protected void RadGridResults_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)//if the item is in edit mode
            {
                GridEditFormItem editItem = (GridEditFormItem)e.Item;
                RadComboBox comboBoxDealersTypes = (RadComboBox)editItem.FindControl("RadComboBoxDealersTypes");
                comboBoxDealersTypes.AutoPostBack = true;
                comboBoxDealersTypes.SelectedIndexChanged += comboBoxDealersTypes_SelectedIndexChanged;
            }
            //if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)//if the item is in insert mode
            //{
            //    GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
            //    RadComboBox combo = (RadComboBox)insertItem.FindControl("RadComboBox1");
            //    combo.AutoPostBack = true;
            //    combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged);
            //}

            //pager
            if (e.Item is GridPagerItem)
            {
                GridPagerItem item = e.Item as GridPagerItem;
                Button changePageSize = (Button)e.Item.FindControl("ChangePageSizeLinkButton");
                changePageSize.Text = "تغيير";
                Button goTO = (Button)e.Item.FindControl("GoToPageLinkButton");
                goTO.Text = "تغيير";
                Label itesNumberLabel = (Label)e.Item.FindControl("ChangePageSizeLabel");
                itesNumberLabel.Text = "          عدد الصفوف :";
                Label goTOLabel = (Label)e.Item.FindControl("GoToPageLabel");
                goTOLabel.Text = "صفحة :";
                Label ofLabel = (Label)e.Item.FindControl("PageOfLabel");
                ofLabel.Text = "من " + item.Paging.PageCount.ToString();
            }
        }
        protected void RadGridResults_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Contacts")
            {
                this.ShowWindow("WebFormPersons.aspx?" + BLDealer.PropertiesNames.Dealer_ID + "=" + e.CommandArgument 
                    + "&" + BLDealer.PropertiesNames.DealerType_ID + "=" + this.DealerType_ID);
            }
            if (e.CommandName == "Addresses")
            {
                this.ShowWindow("WebFormAddresses.aspx?" + BLDealer.PropertiesNames.Dealer_ID + "=" + e.CommandArgument
                    + "&" + BLDealer.PropertiesNames.DealerType_ID + "=" + this.DealerType_ID);
            }
            if (e.CommandName == "Products")
            {
                this.ShowWindow("WebFormDealersProducts.aspx?" + BLDealer.PropertiesNames.Dealer_ID + "=" + e.CommandArgument
                    + "&" + BLDealer.PropertiesNames.DealerType_ID + "=" + this.DealerType_ID);
            }
            if (e.CommandName == "BanksAcounts")
            {
                this.ShowWindow("WebFormDealersBanks.aspx?" + BLDealer.PropertiesNames.Dealer_ID + "=" + e.CommandArgument
                    + "&" + BLDealer.PropertiesNames.DealerType_ID + "=" + this.DealerType_ID);
            }
            if (e.CommandName == "Attachments")
            {
                this.ShowWindow("WebFormDealersAttachments.aspx?" + BLDealer.PropertiesNames.Dealer_ID + "=" + e.CommandArgument
                    + "&" + BLDealer.PropertiesNames.DealerType_ID + "=" + this.DealerType_ID);
            }

            //set default value
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                e.Canceled = true;
                System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
                newValues[BLDealer.PropertiesNames.Dealer_StartDate] = DateTime.Now.Date;
                e.Item.OwnerTableView.InsertItem(newValues);
            }

        }
        protected void RadGridResults_InsertCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            BLDealer dealer = new BLDealer();
            if (newValues[BLDealer.PropertiesNames.Dealer_Code] != null)
            {
                dealer.Dealer_Code = newValues[BLDealer.PropertiesNames.Dealer_Code].ToString();
            }
            if (newValues[BLDealer.PropertiesNames.Dealer_NameAr] != null && newValues[BLDealer.PropertiesNames.Dealer_NameAr].ToString().Trim() != "")
            {
                dealer.Dealer_NameAr = newValues[BLDealer.PropertiesNames.Dealer_NameAr].ToString();
            }
            else
            {
                this.ShowNotification("من فضلك قم بادخال الاسم", NotificationIconType.Warning);
                e.Canceled = true;
                return;
            }
            if (newValues[BLDealer.PropertiesNames.Dealer_NameEn] != null)
            {
                dealer.Dealer_NameEn = newValues[BLDealer.PropertiesNames.Dealer_NameEn].ToString();
            }
            RadComboBox radComboBoxImportance = (RadComboBox)editedItem.FindControl("RadComboBoxImportance");
            if (radComboBoxImportance.SelectedValue != "")
            {
                dealer.Importance_ID = new Guid(radComboBoxImportance.SelectedValue);
            }
            RadComboBox radComboBoxTypeoftrade = (RadComboBox)editedItem.FindControl("RadComboBoxTypeOfTrade");
            if (radComboBoxTypeoftrade.SelectedValue != "")
            {
                dealer.TypeOfTrade_ID = new Guid(radComboBoxTypeoftrade.SelectedValue);
            }
            RadComboBox radComboBoxDealersTypes = (RadComboBox)editedItem.FindControl("RadComboBoxDealersTypes");
            if (radComboBoxDealersTypes.SelectedValue != "")
            {
                dealer.DealerType_ID = new Guid(radComboBoxDealersTypes.SelectedValue);
            }
            RadComboBox radComboBoxDealersClassifications = (RadComboBox)editedItem.FindControl("RadComboBoxDealersClassifications");
            if (radComboBoxDealersClassifications.SelectedValue != "")
            {
                dealer.DealerClassification_ID = new Guid(radComboBoxDealersClassifications.SelectedValue);
            }
            if (Convert.ToDateTime(newValues[BLDealer.PropertiesNames.Dealer_StartDate]).Year > 1900)
            {
                dealer.Dealer_StartDate = Convert.ToDateTime(newValues[BLDealer.PropertiesNames.Dealer_StartDate]);
            }
            if (newValues[BLDealer.PropertiesNames.Dealer_Notes] != null)
            {
                dealer.Dealer_Notes = newValues[BLDealer.PropertiesNames.Dealer_Notes].ToString();
            }
            dealer.Dealer_Search = StringHelper.ToUniqueStatement(dealer.Dealer_Code + dealer.Dealer_NameAr + dealer.Dealer_NameEn + dealer.Dealer_Notes);
            dealer.AutoSave();
            if (dealer.Error == null)
            {
                this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحفظ", NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void RadGridResults_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            Hashtable newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            BLDealer dealer = new BLDealer();
            dealer.Dealer_ID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BLDealer.PropertiesNames.Dealer_ID].ToString());
            dealer.LoadByIdentifiers();
            if (newValues[BLDealer.PropertiesNames.Dealer_Code] != null)
            {
                dealer.Dealer_Code = newValues[BLDealer.PropertiesNames.Dealer_Code].ToString();
            }
            if (newValues[BLDealer.PropertiesNames.Dealer_NameAr] != null && newValues[BLDealer.PropertiesNames.Dealer_NameAr].ToString().Trim() != "")
            {
                dealer.Dealer_NameAr = newValues[BLDealer.PropertiesNames.Dealer_NameAr].ToString();
            }
            else
            {
                this.ShowNotification("من فضلك قم بادخال الاسم", NotificationIconType.Warning);
                e.Canceled = true;
                return;
            }
            if (newValues[BLDealer.PropertiesNames.Dealer_NameEn] != null)
            {
                dealer.Dealer_NameEn = newValues[BLDealer.PropertiesNames.Dealer_NameEn].ToString();
            }
            RadComboBox radComboBoxImportance = (RadComboBox)editedItem.FindControl("RadComboBoxImportance");
            if (radComboBoxImportance.SelectedValue != "")
            {
                dealer.Importance_ID = new Guid(radComboBoxImportance.SelectedValue);
            }
            RadComboBox radComboBoxTypeoftrade = (RadComboBox)editedItem.FindControl("RadComboBoxTypeOfTrade");
            if (radComboBoxTypeoftrade.SelectedValue != "")
            {
                dealer.TypeOfTrade_ID = new Guid(radComboBoxTypeoftrade.SelectedValue);
            }
            RadComboBox radComboBoxDealersTypes = (RadComboBox)editedItem.FindControl("RadComboBoxDealersTypes");
            if (radComboBoxDealersTypes.SelectedValue != "")
            {
                dealer.DealerType_ID = new Guid(radComboBoxDealersTypes.SelectedValue);
            }
            RadComboBox radComboBoxDealersClassifications = (RadComboBox)editedItem.FindControl("RadComboBoxDealersClassifications");
            if (radComboBoxDealersClassifications.SelectedValue != "")
            {
                dealer.DealerClassification_ID = new Guid(radComboBoxDealersClassifications.SelectedValue);
            }
            if (Convert.ToDateTime(newValues[BLDealer.PropertiesNames.Dealer_StartDate]).Year > 1900)
            {
                dealer.Dealer_StartDate = Convert.ToDateTime(newValues[BLDealer.PropertiesNames.Dealer_StartDate]);
            }
            if (newValues[BLDealer.PropertiesNames.Dealer_Notes] != null)
            {
                dealer.Dealer_Notes = newValues[BLDealer.PropertiesNames.Dealer_Notes].ToString();
            }
            dealer.Dealer_Search = StringHelper.ToUniqueStatement(dealer.Dealer_Code + dealer.Dealer_NameAr + dealer.Dealer_NameEn + dealer.Dealer_Notes);
            dealer.AutoSave();
            if (dealer.Error == null)
            {
                this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحفظ", NotificationIconType.Error);
                e.Canceled = true;
                return;
            }
        }
        protected void RadGridResults_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            BLDealer dealer = new BLDealer();
            dealer.Dealer_ID = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][BLDealer.PropertiesNames.Dealer_ID].ToString());
            dealer.DeleteByIdentifiers();
            if (dealer.Error == null)
            {
                this.ShowNotification("تم الحذف بنجاح", NotificationIconType.Ok);
            }
            else
            {
                this.ShowNotification("حدث خطا اثناء الحذف", NotificationIconType.Error);
                return;
            }
        }
        protected void RadGridResults_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                //combobox importance
                RadComboBox radComboBoxImportance = (RadComboBox)e.Item.FindControl("RadComboBoxImportance");
                radComboBoxImportance.DataSource = CashVariables.ImportanceCollection;
                radComboBoxImportance.DataTextField = BLImportance.PropertiesNames.Importance_NameAr;
                radComboBoxImportance.DataValueField = BLImportance.PropertiesNames.Importance_ID;
                radComboBoxImportance.DataBind();
                if (e.Item.ItemIndex != -1)
                {
                    BLDealer dealer = (BLDealer)e.Item.DataItem;
                    radComboBoxImportance.SelectedValue = dealer.Importance_ID.ToString();
                }
                //combobox typeOftrade
                RadComboBox radComboBoxTypeOfTrade = (RadComboBox)e.Item.FindControl("RadComboBoxTypeOfTrade");
                radComboBoxTypeOfTrade.DataSource = CashVariables.TypeOfTradeCollection;
                radComboBoxTypeOfTrade.DataTextField = BLTypeOfTrade.PropertiesNames.TypeOfTrade_NameAr;
                radComboBoxTypeOfTrade.DataValueField = BLTypeOfTrade.PropertiesNames.TypeOfTrade_ID;
                radComboBoxTypeOfTrade.DataBind();
                if (e.Item.ItemIndex != -1)
                {
                    BLDealer dealer = (BLDealer)e.Item.DataItem;
                    radComboBoxTypeOfTrade.SelectedValue = dealer.TypeOfTrade_ID.ToString();
                }
                //combobox DealersTypes
                RadComboBox radComboBoxDealersTypes = (RadComboBox)e.Item.FindControl("RadComboBoxDealersTypes");
                radComboBoxDealersTypes.DataSource = CashVariables.DealersTypeCollection;
                radComboBoxDealersTypes.DataTextField = BLDealersType.PropertiesNames.DealerType_NameAr;
                radComboBoxDealersTypes.DataValueField = BLDealersType.PropertiesNames.DealerType_ID;
                radComboBoxDealersTypes.DataBind();
                radComboBoxDealersTypes.SelectedValue = this.DealerType_ID.ToString();

                //combobox DealersClassifications
                RadComboBox radComboBoxDealersClassifications = (RadComboBox)e.Item.FindControl("RadComboBoxDealersClassifications");
                var items = (from i in CashVariables.DealersClassificationCollection where i.DealerType_ID == new Guid(radComboBoxDealersTypes.SelectedValue) select i);
                BLDealersClassificationCollection DealersClassificationCollection = new BLDealersClassificationCollection(items);
                radComboBoxDealersClassifications.DataSource = DealersClassificationCollection;
                radComboBoxDealersClassifications.DataValueField = BLDealersClassification.PropertiesNames.DealerClassification_ID;
                radComboBoxDealersClassifications.DataTextField = BLDealersClassification.PropertiesNames.DealerClassification_NameAr;
                radComboBoxDealersClassifications.DataBind();

                
                //width
                GridEditableItem editedItem = e.Item as GridEditableItem;
                GridEditManager editMan = editedItem.EditManager;
                GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)(editMan.GetColumnEditor(BLDealer.PropertiesNames.Dealer_Notes));
                editor.TextBoxMode = TextBoxMode.MultiLine;
                editor.TextBoxControl.Width = Unit.Pixel(300);
                editor.TextBoxControl.Height = Unit.Pixel(40);
            }
        }
        protected void comboBoxDealersTypes_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            RadComboBox radComboBoxDealersTypes = (RadComboBox)sender;
            GridEditableItem editedItem = radComboBoxDealersTypes.NamingContainer as GridEditableItem;
            RadComboBox radComboBoxDealersClassifications = (RadComboBox)editedItem["DealerClassification_ID"].FindControl("RadComboBoxDealersClassifications");
            var items=(from i in CashVariables.DealersClassificationCollection where i.DealerType_ID==new  Guid(radComboBoxDealersTypes.SelectedValue) select i);
            BLDealersClassificationCollection DealersClassificationCollection = new BLDealersClassificationCollection(items);
            radComboBoxDealersClassifications.DataSource = DealersClassificationCollection;
            radComboBoxDealersClassifications.DataValueField = BLDealersClassification.PropertiesNames.DealerClassification_ID;
            radComboBoxDealersClassifications.DataTextField = BLDealersClassification.PropertiesNames.DealerClassification_NameAr;
            radComboBoxDealersClassifications.DataBind();
        }

        private void getDealers()
        {
            BLDealerCollection dealerCollection = new BLDealerCollection();
            //sort
            dealerCollection.Template.AddSort(BLDealer.PropertiesNames.Dealer_RowNumber, false);
            
            //search filter
            dealerCollection.Template.AddFilter(LogicalOperators.And, BLDealer.PropertiesNames.Dealer_Search, FilterOperators.Contains, this.RadTextBoxSearch.Text, typeof(string));
            
            //importance filter
            if (this.RadComboBoxImportance.SelectedValue == "11111111-1111-1111-1111-111111111111")
            {

            }
            else if (this.RadComboBoxImportance.SelectedValue == "00000000-0000-0000-0000-000000000000")
            {
                dealerCollection.Template.AddFilter(LogicalOperators.And, BLDealer.PropertiesNames.Importance_ID, FilterOperators.Undefined, null, typeof(Guid));
            }
            else
            {
                dealerCollection.Template.AddFilter(LogicalOperators.And, BLDealer.PropertiesNames.Importance_ID, FilterOperators.Equal, new Guid(this.RadComboBoxImportance.SelectedValue), typeof(Guid));
            }

            //TypeOfTrade filter
            if (this.RadComboBoxTypeOfTrade.SelectedValue == "11111111-1111-1111-1111-111111111111")
            {

            }
            else if (this.RadComboBoxTypeOfTrade.SelectedValue == "00000000-0000-0000-0000-000000000000")
            {
                dealerCollection.Template.AddFilter(LogicalOperators.And, BLDealer.PropertiesNames.TypeOfTrade_ID, FilterOperators.Undefined, null, typeof(Guid));
            }
            else
            {
                dealerCollection.Template.AddFilter(LogicalOperators.And, BLDealer.PropertiesNames.TypeOfTrade_ID, FilterOperators.Equal, new Guid(this.RadComboBoxTypeOfTrade.SelectedValue), typeof(Guid));
            }

            //dateFrom filter
            if (this.RadDatePickerFrom.SelectedDate != null)
            {
                dealerCollection.Template.AddFilter(LogicalOperators.And, BLDealer.PropertiesNames.Dealer_StartDate, FilterOperators.GreaterThanOrEqual, this.RadDatePickerFrom.SelectedDate, typeof(DateTime));
            }

            //dateTo filter
            if (this.RadDatePickerTo.SelectedDate != null)
            {
                dealerCollection.Template.AddFilter(LogicalOperators.And, BLDealer.PropertiesNames.Dealer_StartDate, FilterOperators.LessThanOrEqual, this.RadDatePickerTo.SelectedDate, typeof(DateTime));
            }
            

            //dealerTypeClassifications
            dealerCollection.Template.AddFilter(LogicalOperators.And, BLDealer.PropertiesNames.DealerType_ID, FilterOperators.Equal, this.DealerType_ID,typeof(Guid));

            //dealerClassifications filter
            if (this.RadComboBoxDealersClassifications.SelectedValue == "11111111-1111-1111-1111-111111111111")
            {

            }
            else if (this.RadComboBoxImportance.SelectedValue == "00000000-0000-0000-0000-000000000000")
            {
                dealerCollection.Template.AddFilter(LogicalOperators.And, BLDealer.PropertiesNames.DealerClassification_ID, FilterOperators.Undefined, null, typeof(Guid));
            }
            else
            {
                dealerCollection.Template.AddFilter(LogicalOperators.And, BLDealer.PropertiesNames.DealerClassification_ID, FilterOperators.Equal, new Guid(this.RadComboBoxDealersClassifications.SelectedValue), typeof(Guid));
            }

            //load
            dealerCollection.Load();

            
            //Product filter with linq
            if (this.RadComboBoxProducts.SelectedValue == "11111111-1111-1111-1111-111111111111")
            {

            }
            else if (this.RadComboBoxProducts.SelectedValue == "00000000-0000-0000-0000-000000000000")
            {
                BLDealer_ProductCollection Dealer_ProductCollection=new BLDealer_ProductCollection ();
                Dealer_ProductCollection.Load();
                List<Guid> IDs=(from i in dealerCollection join j in Dealer_ProductCollection on i.Dealer_ID equals j.Dealer_ID orderby i.Dealer_NameAr select i.Dealer_ID.Value).ToList<Guid>();
                BLDealerCollection newDealerCollection = new BLDealerCollection();
                for (int i = 0; i < dealerCollection.Count; i++)
                {
                    if(!IDs.Contains(dealerCollection[i].Dealer_ID.Value))
                    {
                        newDealerCollection.Add(dealerCollection[i]);
                    }
                    
                }
                dealerCollection = newDealerCollection;
            }
            else
            {
                BLDealer_ProductCollection Dealer_ProductCollection = new BLDealer_ProductCollection();
                Dealer_ProductCollection.Load();
                dealerCollection = new BLDealerCollection((from i in dealerCollection join j in Dealer_ProductCollection on i.Dealer_ID equals j.Dealer_ID where j.Product_ID.ToString() == this.RadComboBoxProducts.SelectedValue orderby i.Dealer_NameAr select i));
            }


            //Country filter with linq
            if (this.RadComboBoxCountries.SelectedValue == "11111111-1111-1111-1111-111111111111")
            {

            }
            else if (this.RadComboBoxCountries.SelectedValue == "00000000-0000-0000-0000-000000000000")
            {
                BLAddresseCollection addressCollection = new BLAddresseCollection();
                addressCollection.Load();
                List<Guid> IDs = (from i in dealerCollection join j in addressCollection on i.Dealer_ID equals j.Dealer_ID orderby i.Dealer_NameAr select i.Dealer_ID.Value).ToList<Guid>();
                BLDealerCollection newDealerCollection = new BLDealerCollection();
                for (int i = 0; i < dealerCollection.Count; i++)
                {
                    if (!IDs.Contains(dealerCollection[i].Dealer_ID.Value))
                    {
                        newDealerCollection.Add(dealerCollection[i]);
                    }

                }
                dealerCollection = newDealerCollection;
            }
            else
            {
                BLAddresseCollection addressCollection = new BLAddresseCollection();
                addressCollection.Load();
                dealerCollection = new BLDealerCollection((from i in dealerCollection join j in addressCollection on i.Dealer_ID equals j.Dealer_ID where j.Country_ID.ToString() == this.RadComboBoxCountries.SelectedValue orderby i.Dealer_NameAr select i));
            }



            for (int i = 0; i < dealerCollection.Count; i++)
            {
                int index = CashVariables.ImportanceCollection.FindByIentifier(dealerCollection[i].Importance_ID);
                if (index != -1)
                {
                    dealerCollection[i].Importance_NameAr = CashVariables.ImportanceCollection[index].Importance_NameAr;
                }
                index = CashVariables.TypeOfTradeCollection.FindByIentifier(dealerCollection[i].TypeOfTrade_ID);
                if (index != -1)
                {
                    dealerCollection[i].TypeOfTrade_NameAr = CashVariables.TypeOfTradeCollection[index].TypeOfTrade_NameAr;
                }
                index = CashVariables.DealersTypeCollection.FindByIentifier(dealerCollection[i].DealerType_ID);
                if (index != -1)
                {
                    dealerCollection[i].DealerType_NameAr = CashVariables.DealersTypeCollection[index].DealerType_NameAr;
                }
                index = CashVariables.DealersClassificationCollection.FindByIentifier(dealerCollection[i].DealerClassification_ID);
                if (index != -1)
                {
                    dealerCollection[i].DealerClassification_NameAr = CashVariables.DealersClassificationCollection[index].DealerClassification_NameAr;
                }
            }
            
            if (dealerCollection.Error != null)
            {
                this.ShowNotification("حدث خطا اثناء استعراض البيانات", NotificationIconType.Error);
            }
            this.RadGridResults.DataSource = dealerCollection;
            this.RadGridResults.MasterTableView.DataKeyNames = BLDealer.PropertiesNames.Identifiers;
        }
        private void getImportanceFilter()
        {
            BLImportanceCollection importanceCollection = new BLImportanceCollection(CashVariables.ImportanceCollection);
            importanceCollection.Insert(0, new BLImportance { Importance_ID = new Guid("00000000-0000-0000-0000-000000000000"), Importance_NameAr = "بدون" });
            importanceCollection.Insert(0, new BLImportance { Importance_ID = new Guid("11111111-1111-1111-1111-111111111111"), Importance_NameAr = "الكل" });
            this.RadComboBoxImportance.DataSource = importanceCollection;
            this.RadComboBoxImportance.DataTextField = BLImportance.PropertiesNames.Importance_NameAr;
            this.RadComboBoxImportance.DataValueField = BLImportance.PropertiesNames.Importance_ID;
            this.RadComboBoxImportance.DataBind();
        }
        private void getTypeOfTradeFilter()
        {
            BLTypeOfTradeCollection TypeOfTradeCollection = new BLTypeOfTradeCollection(CashVariables.TypeOfTradeCollection);
            TypeOfTradeCollection.Insert(0, new BLTypeOfTrade { TypeOfTrade_ID = new Guid("00000000-0000-0000-0000-000000000000"), TypeOfTrade_NameAr = "بدون" });
            TypeOfTradeCollection.Insert(0, new BLTypeOfTrade { TypeOfTrade_ID = new Guid("11111111-1111-1111-1111-111111111111"), TypeOfTrade_NameAr = "الكل" });
            this.RadComboBoxTypeOfTrade.DataSource = TypeOfTradeCollection;
            this.RadComboBoxTypeOfTrade.DataTextField = BLTypeOfTrade.PropertiesNames.TypeOfTrade_NameAr;
            this.RadComboBoxTypeOfTrade.DataValueField = BLTypeOfTrade.PropertiesNames.TypeOfTrade_ID;
            this.RadComboBoxTypeOfTrade.DataBind();
        }
        private void getDealersClassificationsFilter()
        {
            var items = (from i in CashVariables.DealersClassificationCollection where i.DealerType_ID==this.DealerType_ID select i);
            BLDealersClassificationCollection DealersClassificationCollection = new BLDealersClassificationCollection(items);
            DealersClassificationCollection.Insert(0, new BLDealersClassification { DealerClassification_ID = new Guid("00000000-0000-0000-0000-000000000000"), DealerClassification_NameAr = "بدون" });
            DealersClassificationCollection.Insert(0, new BLDealersClassification { DealerClassification_ID = new Guid("11111111-1111-1111-1111-111111111111"), DealerClassification_NameAr = "الكل" });
            this.RadComboBoxDealersClassifications.DataSource = DealersClassificationCollection;
            this.RadComboBoxDealersClassifications.DataTextField = BLDealersClassification.PropertiesNames.DealerClassification_NameAr;
            this.RadComboBoxDealersClassifications.DataValueField = BLDealersClassification.PropertiesNames.DealerClassification_ID;
            this.RadComboBoxDealersClassifications.DataBind();
        }
        private void getProductsFilter()
        {
            BLProductCollection productCollection = new BLProductCollection(CashVariables.ProductCollection);
            productCollection.Insert(0, new BLProduct { Product_ID = new Guid("00000000-0000-0000-0000-000000000000"), Product_NameAr = "بدون" });
            productCollection.Insert(0, new BLProduct { Product_ID = new Guid("11111111-1111-1111-1111-111111111111"), Product_NameAr = "الكل" });
            this.RadComboBoxProducts.DataSource = productCollection;
            this.RadComboBoxProducts.DataTextField = BLProduct.PropertiesNames.Product_NameAr;
            this.RadComboBoxProducts.DataValueField = BLProduct.PropertiesNames.Product_ID;
            this.RadComboBoxProducts.DataBind();
        }
        private void getCountryFilter()
        {
            BLCountryCollection CountryCollection = new BLCountryCollection(CashVariables.CountryCollection);
            CountryCollection.Insert(0, new BLCountry { Country_ID = new Guid("00000000-0000-0000-0000-000000000000"), Country_NameAr = "بدون" });
            CountryCollection.Insert(0, new BLCountry { Country_ID = new Guid("11111111-1111-1111-1111-111111111111"), Country_NameAr = "الكل" });
            this.RadComboBoxCountries.DataSource = CountryCollection;
            this.RadComboBoxCountries.DataTextField = BLCountry.PropertiesNames.Country_NameAr;
            this.RadComboBoxCountries.DataValueField = BLCountry.PropertiesNames.Country_ID;
            this.RadComboBoxCountries.DataBind();
        }
    }
}